package com.lyy.weather.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.lyy.weather.R;
import com.lyy.weather.bean.City;
import com.lyy.weather.layout.adapter.AddCityAdapter;

import java.util.ArrayList;
import java.util.List;

public class WeatherAddingFragment extends Fragment {
    //编辑框
   private EditText editText;
    //城市列表
    private ListView citylist;
    //缓存城市的列表
    private List<City> cityHolder;
    //所属的活动
    private Activity activity;
    //适配器
    private AddCityAdapter addCityAdapter;

    private FragmentManager fragmentManager;


    public WeatherAddingFragment(FragmentManager Manager) {
        fragmentManager=Manager;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.weather_adding_fragment,container,false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        activity=getActivity();
        cityHolder=new ArrayList<>();
        editText=(EditText)activity.findViewById(R.id.search_city_edittext_adding_fragment);
        citylist=(ListView)activity.findViewById(R.id.citylist_adding_fragment);
        //子项点击事件
        citylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //点到子项就把该子项的信息加入数据库
                City city=addCityAdapter.getItem(position);


                //加入数据后回到更换城市的碎片
                Toast.makeText(activity, "增加城市成功", Toast.LENGTH_SHORT).show();
                replaceFragment(new WeatherChangeFragment(fragmentManager));

            }
        });
        addCityAdapter=new AddCityAdapter(activity,R.layout.city_item_change,cityHolder);
        citylist.setAdapter(addCityAdapter);
       // editText.setOnFocusChangeListener();
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
               // Toast.makeText(activity, "1", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Toast.makeText(activity, "2", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void afterTextChanged(Editable s) {
                //Toast.makeText(activity, "3", Toast.LENGTH_SHORT).show();
            }
        });


    }

    /**
     * 展示搜索到的城市
     * 改变城市缓存，刷新listview
     */
    private void refreshList(){
        //
    }

    /**
     * 更换碎片
     */
    public void replaceFragment(Fragment fragment){

        //3.开启一个事务，通过调用beginTransaction()方法开启
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        //4.向容器内添加或替换碎片，一般使用replace()方法实现，需要传入容器的id和待添加的碎片实例
        transaction.replace(R.id.weather_change_fragment_layout,fragment);
        //5.提交事务，调用commit()方法完成
        transaction.commit();
    }
}
